/*
	Interface Program for the Linux Driver for 
	Enterpoint's Raggedstone1 FPGA PCI Board with the lcpd-scope core
	This demo programm alows you to show datas from a adc0820 on gnuplot.

	Written by T.Kurmann, R. Woodtli, S. Eichenberger <kurmt1_AT_bfh.ch,
	woodr1_AT_bfh.ch, eichs2_AT_bfh.ch>
	Based on examples of Manuel Bessler <m.bessler_AT_gmx.net>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or 
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <stdio.h>
#include <fcntl.h> 
#include <unistd.h>
#include <sys/ioctl.h>  
#include <netinet/in.h>
#include <stdlib.h>
#include "gnuplot_i.h"

//Buf lenght
#define DATALEN 4000

//Defines to comunicate with the Driver
#define MAJOR_NUM 100
#define IOCTL_SETDPY _IOR(MAJOR_NUM, 0, short int)
#define IOCTL_SETGPIO _IOR(MAJOR_NUM, 2, short int)
#define DEVICE_NAME "/dev/lcpd-scope"

/**
 * \brief  	Test Programm
 *
 *			This is a Testprogramm for the lcpd_scope_driver and the adc0820
 *
 * \param	int   	nothing
 * \param	char**	nothing
 *
 * \return	      	Success or not
 *
 */
main(int argc, char ** argv)
{
	int file_desc, ret_val;
	char *msg = "Message passed by ioctl\n";
	short int val = 0xDEAD;
	int Buffer[DATALEN];
	double Buffer2[DATALEN];
	int i;
	gnuplot_ctrl    *   h1;

	file_desc = open(DEVICE_NAME, 0);
	if (file_desc < 0) 
	{
		printf ("Can't open device file: %s\n", DEVICE_NAME);
		exit(-1);
	}
	//init guplot
	h1 = gnuplot_init() ;
	gnuplot_setstyle(h1, "points") ;
	for (i=0;i<1000;i++)
	{
		read (file_desc,Buffer,DATALEN);
		int i;
		for (i=0;i<DATALEN;i++)
		{
			Buffer2[i] = 5.0/255*(double)Buffer[i];
		}	
		//plot data with gnuplot
		gnuplot_plot_x(h1,Buffer2,DATALEN,"linuxPlot");
		//refresh only every 2 seconds
		sleep(2);
		gnuplot_resetplot(h1) ;
	}
	close(file_desc); 
	
}
